{%- assign taboolaPixels = storeConfig.taboola | strtoarray -%}
{% assign adwords = storeConfig.google_adwords_trace_id | strtoarray %}
{% assign tracking = storeConfig.google_analytics_trace_id | strtoarray %}
{% assign conversion = storeConfig.google_conversion_id | strtoarray %}

{%- assign bigo_pixels = storeConfig.bigo_pixel_id | strtoarray -%}
{%- assign vk_pixels = storeConfig.vkontakte | strtoarray -%}
{%- assign yandex_pixels = storeConfig.yandex | strtoarray -%}
{%- assign outbrain_pixels = storeConfig.outbrain | strtoarray -%}
{%- assign criteo_pixels = storeConfig.criteo_pixel_id | strtoarray -%}


{%- assign yahoo_pixels = storeConfig.yahoo_pixel_id -%}


<script type="text/javascript">
  window['TOP_GLOBAL_CALLBACK_LAYER'] = {
    on(id) {
        const that = this;
        return function(...params) {
            if (Array.isArray(that[id])) {
                that[id].forEach(fun => fun.apply(this, params));
            }
        };
    },
    add(id, fun) {
        if (!this[id]) {
            this[id] = [];
        }
        this[id].push(fun);
    },
    register(pixelObj) {
        if (pixelObj && typeof pixelObj === 'object') {
            Object.entries(pixelObj).forEach(([key, value]) => {
                this.add(key, value);
            });
        }
    }
};

    function callback_add_to_cart(ec_data) {
        window['TOP_GLOBAL_CALLBACK_LAYER'].on('add_to_cart')(ec_data);
        if (window['moi']) {
            moi.onEvent('callback_add_to_cart', ec_data);
        }
        console.log('callback_add_to_cart');
        console.log(ec_data);
        try {
            if (typeof callback_google_add_to_cart === 'function') {
                callback_google_add_to_cart(ec_data);
            }
            if (typeof callback_facebook_add_to_cart === 'function') {
                callback_facebook_add_to_cart(ec_data);
            }
            if (typeof callback_kwai_add_to_cart === 'function') {
                callback_kwai_add_to_cart(ec_data);
            }
            if (typeof callback_yahoo_add_to_cart === 'function') {
                callback_yahoo_add_to_cart(ec_data);
            }
            if (typeof callback_taboola_add_to_cart === 'function') {
                callback_taboola_add_to_cart(ec_data);
            }
            if (typeof callback_event_script_add_to_cart === 'function') {
                callback_event_script_add_to_cart(ec_data);
            }
            if (typeof callback_bigo_add_to_cart === 'function') {
                callback_bigo_add_to_cart(ec_data);
            }
            if (typeof callback_vk_add_to_cart === 'function') {
                callback_vk_add_to_cart(ec_data);
            }
            if (typeof callback_yandex_add_to_cart === 'function') {
                callback_yandex_add_to_cart(ec_data);
            }
            if (typeof callback_outbrain_add_to_cart === 'function') {
                callback_outbrain_add_to_cart(ec_data);
            }
            if (typeof callback_criteo_add_to_cart === 'function') {
                callback_criteo_add_to_cart(ec_data);
            }
        } catch (e) {
            console.log(e);
        }
    }

    //remove_from_cart
    function callback_remove_from_cart(ec_data) {
        window['TOP_GLOBAL_CALLBACK_LAYER'].on('remove_from_cart')(ec_data);
        if (window['moi']) {
            moi.onEvent('callback_remove_from_cart', ec_data);
        }
        console.log('callback_remove_from_cart');
        console.log(ec_data);
        try {
            if (typeof callback_google_remove_from_cart === 'function') {
                callback_google_remove_from_cart(ec_data);
            }
            if (typeof callback_facebook_remove_from_cart === 'function') {
                callback_facebook_remove_from_cart(ec_data);
            }
            if (typeof callback_kwai__remove_from_cart === 'function') {
                callback_kwai__remove_from_cart(ec_data);
            }
            if (typeof callback_yahoo_remove_from_cart === 'function') {
                callback_yahoo_remove_from_cart(ec_data);
            }
            if (typeof callback_event_script_remove_from_cart === 'function') {
                callback_event_script_remove_from_cart(ec_data);
            }
        } catch (e) {
            console.log(e);
        }
    }

    function callback_login() {
        window['TOP_GLOBAL_CALLBACK_LAYER'].on('login')();
        if (window['moi']) {
            moi.onEvent('callback_login');
        }
        console.log('callback_login');
        try {
            if (typeof callback_google_login === 'function') {
                callback_google_login();
            }
            if (typeof callback_facebook_login === 'function') {
                callback_facebook_login();
            }
            if (typeof callback_kwai_login === 'function') {
                callback_kwai_login();
            }
            if (typeof callback_yahoo_login === 'function') {
                callback_yahoo_login();
            }
            if (typeof callback_script_event_login === 'function') {
                callback_script_event_login();
            }
        } catch (e) {
            console.log(e);
        }
    }

    function callback_sign_up() {
        window['TOP_GLOBAL_CALLBACK_LAYER'].on('sign_up')();
        if (window['moi']) {
            moi.onEvent('callback_sign_up');
        }
        console.log('callback_sign_up');
        try {
            if (typeof callback_google_sign_up === 'function') {
                callback_google_sign_up();
            }
            if (typeof callback_facebook_sign_up === 'function') {
                callback_facebook_sign_up();
            }
            if (typeof callback_kwai_sign_up === 'function') {
                callback_kwai_sign_up();
            }
            if (typeof callback_yahoo_sign_up === 'function') {
                callback_yahoo_sign_up();
            }
            if (typeof callback_taboola_sign_up === 'function') {
                callback_taboola_sign_up();
            }
            if (typeof callback_bigo_sign_up === 'function') {
                callback_bigo_sign_up();
            }
            if (typeof callback_vk_sign_up === 'function') {
                callback_vk_sign_up();
            }
            if (typeof callback_yandex_sign_up === 'function') {
                callback_yandex_sign_up();
            }
            if (typeof callback_event_script_sign_up === 'function') {
                callback_event_script_sign_up();
            }
        } catch (e) {
            console.log(e);
        }
    }

    function callback_share_product(ec_data, platform) {
        window['TOP_GLOBAL_CALLBACK_LAYER'].on('share_product')(ec_data, platform);
        if (window['moi']) {
            moi.onEvent('callback_share_product', { ec_data: ec_data, platform: platform });
        }
        console.log('callback_share_product');
        console.log(ec_data);
        console.log(platform);
        try {
            if (typeof callback_google_share_product === 'function') {
                callback_google_share_product(ec_data, platform);
            }
            if (typeof callback_facebook_share_product === 'function') {
                callback_facebook_share_product(ec_data, platform);
            }
            if (typeof callback_kwai_share_product === 'function') {
                callback_kwai_share_product(ec_data, platform);
            }
            if (typeof callback_yahoo_share_product === 'function') {
                callback_yahoo_share_product(ec_data, platform);
            }
            if (typeof callback_event_script_share_product === 'function') {
                callback_event_script_share_product(ec_data, platform);
            }
            if (typeof callback_base_share_product === 'function') {
                callback_base_share_product(ec_data, platform);
            }
        } catch (e) {
            console.log(e);
        }
    }

    function callback_generate_lead() {
        window['TOP_GLOBAL_CALLBACK_LAYER'].on('generate_lead')();
        if (window['moi']) {
            moi.onEvent('callback_generate_lead');
        }
        console.log('callback_generate_lead');
        try {
            if (typeof callback_google_generate_lead === 'function') {
                callback_google_generate_lead();
            }
            if (typeof callback_facebook_generate_lead === 'function') {
                callback_facebook_generate_lead();
            }
            if (typeof callback_kwai_generate_lead === 'function') {
                callback_kwai_generate_lead();
            }
            if (typeof callback_yahoo_generate_lead === 'function') {
                callback_yahoo_generate_lead();
            }
            if (typeof callback_event_script_generate_lead === 'function') {
                callback_event_script_generate_lead();
            }
        } catch (e) {
            console.log(e);
        }
    }


    function callback_begin_checkout() {
        window['TOP_GLOBAL_CALLBACK_LAYER'].on('begin_checkout')();
        if (window['moi']) {
            moi.onEvent('callback_begin_checkout');
        }
        console.log('callback_begin_checkout');
        try {
            if (typeof callback_google_begin_checkout === 'function') {
                callback_google_begin_checkout();
            }
            if (typeof callback_facebook_begin_checkout === 'function') {
                callback_facebook_begin_checkout();
            }
            if (typeof callback_kwai_begin_checkout === 'function') {
                callback_kwai_begin_checkout();
            }
            if (typeof callback_yahoo_begin_checkout === 'function') {
                callback_yahoo_begin_checkout();
            }
            if (typeof callback_taboola_begin_checkout === 'function') {
                callback_taboola_begin_checkout();
            }
            if (typeof callback_bigo_begin_checkout === 'function') {
                callback_bigo_begin_checkout();
            }
            if (typeof callback_outbrain_begin_checkout === 'function') {
                callback_outbrain_begin_checkout();
            }
            if (typeof callback_event_script_begin_checkout === 'function') {
                callback_event_script_begin_checkout();
            }
        } catch (e) {
            console.log(e);
        }
    }

    function callback_add_address_information() {
        window['TOP_GLOBAL_CALLBACK_LAYER'].on('add_address_information')();
        if (window['moi']) {
            moi.onEvent('callback_add_address_information');
        }
        console.log('callback_add_address_information');
        try {
            if (typeof callback_google_add_address_information === 'function') {
                callback_google_add_address_information();
            }
            if (typeof callback_facebook_add_address_information === 'function') {
                callback_facebook_add_address_information();
            }
            if (typeof callback_kwai_add_address_information === 'function') {
                callback_kwai_add_address_information();
            }
            if (typeof callback_yahoo_add_address_information === 'function') {
                callback_yahoo_add_address_information();
            }
            if (typeof callback_event_script_add_address_information === 'function') {
                callback_event_script_add_address_information();
            }
        } catch (e) {
            console.log(e);
        }
    }

    function callback_add_shipping_information() {
        window['TOP_GLOBAL_CALLBACK_LAYER'].on('add_shipping_information')();
        if (window['moi']) {
            moi.onEvent('callback_add_shipping_information');
        }
        console.log('callback_add_shipping_information');
        try {
            if (typeof callback_google_add_shipping_information === 'function') {
                callback_google_add_shipping_information();
            }
            if (typeof callback_facebook_add_shipping_information === 'function') {
                callback_facebook_add_shipping_information();
            }
            if (typeof callback_kwai_add_shipping_information === 'function') {
                callback_kwai_add_shipping_information();
            }
            if (typeof callback_yahoo_add_shipping_information === 'function') {
                callback_yahoo_add_shipping_information();
            }
            if (typeof callback_event_script_add_shipping_information === 'function') {
                callback_event_script_add_shipping_information();
            }
        } catch (e) {
            console.log(e);
        }
    }

    function callback_add_payment_information() {
        window['TOP_GLOBAL_CALLBACK_LAYER'].on('add_payment_information')();
        if (window['moi']) {
            moi.onEvent('callback_add_payment_information');
        }
        console.log('callback_add_payment_information');
        try {
            if (typeof callback_google_add_payment_information === 'function') {
                callback_google_add_payment_information();
            }
            if (typeof callback_facebook_add_payment_information === 'function') {
                callback_facebook_add_payment_information();
            }
            if (typeof callback_kwai_add_payment_information === 'function') {
                callback_kwai_add_payment_information();
            }
            if (typeof callback_yahoo_add_payment_information === 'function') {
                callback_yahoo_add_payment_information();
            }
            if (typeof callback_taboola_add_payment_information === 'function') {
                callback_taboola_add_payment_information();
            }
            if (typeof callback_event_script_add_payment_information === 'function') {
                callback_event_script_add_payment_information();
            }
        } catch (e) {
            console.log(e);
        }
    }

    function callback_add_to_wishlist(ec_data) {
        window['TOP_GLOBAL_CALLBACK_LAYER'].on('add_to_wishlist')(ec_data);
        if (window['moi']) {
            moi.onEvent('callback_add_to_wishlist', ec_data);
        }
        console.log('callback_add_to_wishlist');
        try {
            if (typeof callback_google_to_wishlist === 'function') {
                callback_google_to_wishlist(ec_data);
            }
            if (typeof callback_facebook_to_wishlist === 'function') {
                callback_facebook_to_wishlist(ec_data);
            }
            if (typeof callback_kwai_to_wishlist === 'function') {
                callback_kwai_to_wishlist(ec_data);
            }
            if (typeof callback_vk_to_wishlist === 'function') {
                callback_vk_to_wishlist(ec_data);
            }
            if (typeof callback_yandex_to_wishlist === 'function') {
                callback_yandex_to_wishlist(ec_data);
            }
            if (typeof callback_taboola_to_wishlist === 'function') {
                callback_taboola_to_wishlist(ec_data);
            }
        } catch (e) {
            console.log(e);
        }
    }

    function callback_place_an_order(ec_data) {
        window['TOP_GLOBAL_CALLBACK_LAYER'].on('place_an_order')(ec_data);
        if (typeof callback_event_script_place_an_order === 'function') {
            callback_event_script_place_an_order();
        }
    }
</script>

{% include analysis_base %}

{%- if adwords or tracking or conversion -%}
{% include analysis_google %}
{%- endif -%}

{% include analysis_bing %}

{% include analysis_facebook %}

{% include analysis_pinterest %}

{% include analysis_snapchat %}


{% include analysis_tiktok %}


{%- if pixel_config.kwai_pixel_id == true -%}
{% include analysis_kwai %}
{%- endif -%}

{%- if pixel_config.yahoo_pixel == true -%}
{% include analysis_yahoo %}
{%- endif -%}


{%- if taboolaPixels -%}
{% include analysis_taboola %}
{%- endif -%}


{% if bigo_pixels %}
{% include "analysis_bigo",bigo_pixels:bigo_pixels %}
{% endif %}


{% if vk_pixels %}
    {% include "analysis_vk",vk_pixels:vk_pixels %}
{% endif %}
    
{% if yandex_pixels %}
    {% include "analysis_yandex",yandex_pixels:yandex_pixels %}
{% endif %}

{% if outbrain_pixels %}
    {% include "analysis_outbrain",outbrain_pixels:outbrain_pixels %}
{% endif %}

{% if criteo_pixels %}
    {% include "analysis_criteo",criteo_pixels:criteo_pixels %}
{% endif %}


{% include "analysis_twitter" %}
{% include "analysis_mediago" %}
{% include "analysis_newsbreak" %}
{% include "analysis_applovin" %}
{% include "analysis_smartnews" %}
{% include "analysis_reddit" %}
{% include "analysis_google_gtm" %}


{% include analysis_event_script %}

{% comment %}如果url有generate_lead参数  即代表有创建新用户{% endcomment %}
<script type="text/javascript">

document.addEventListener("DOMContentLoaded",function(){

    {%- if template == 'product_detail' -%}
        window['TOP_GLOBAL_CALLBACK_LAYER'].on('view_content')();
    {%- elsif template == 'search_detail' -%}
        window['TOP_GLOBAL_CALLBACK_LAYER'].on('search')();
    {%- elsif controller == 'order' and action == 'success' -%}
        window['TOP_GLOBAL_CALLBACK_LAYER'].on('purchase')();
    {%- elsif template == 'order_payment_gateway' and order.payment_type == 'offlinepay' and storeConfig.is_offlinepay_purchase_pixel == '1' -%}
        window['TOP_GLOBAL_CALLBACK_LAYER'].on('purchase')();
    {%- else -%}
        window['TOP_GLOBAL_CALLBACK_LAYER'].on('page_view')();
    {%- endif -%} 

    {% if current_step == 'contact_information' and template == 'order_contact_information' %}
    callback_begin_checkout();
    {% endif %}

    {% if template == 'orderonepage_index' %}
    callback_begin_checkout();
    document.addEventListener('onePageComplete',function(){
        callback_place_an_order()
    })
    {% endif %}

    {% if current_step == 'shipping_method' and template == 'order_shipping_method' %}
    callback_add_address_information();
    {% endif %}

    {% if current_step == 'payment_method' and template == 'order_payment_method' %}
    callback_add_shipping_information();
    {% endif %}

    {% if current_step == 'payment_gateway' and template == 'order_payment_gateway' %}
    // button event
    //callback_add_payment_information();
    {% endif %}

    {% if request_param.generate_lead %}
    callback_generate_lead();
    {% endif %}

    {% if controller == 'order' and action == 'checkout' and previous_step == 'shipping_method' and add_order == 1 %}
    callback_place_an_order()
    {% endif %}

})
</script>
